/**
 * author:Administrator
 * date:2016/2/23
 * desc:商品快照，
 * 任何商品，都会有一个快照，快照不可修改
 * 如果商品的一些关键属性发生变化，则生成一个新的快照
 * 已支付订单，会绑定到 商品快照上
 */

'use strict';
let Repo = require('./WareRepo.js');
let FormatDate = require('../../../infrastructure/appUtil/FormatDate.js');

class WareSnapshot {
  constructor(pojo) {
    pojo = pojo || {};

    this.id = pojo.id;
    this.wareId = pojo.wareId;            // 原商品的id
    this.ver = pojo.ver;                  // 原商品的ver
    this.vendorId = pojo.vendorId;
    this.categoryId = pojo.categoryId;
    this.name = pojo.name;
    this.price = pojo.price;

    this.imgUrls = pojo.imgUrls;
    this.desc = pojo.desc;
    this.createdAt = pojo.createdAt || FormatDate.unixTimestamp();
  }

  static fromWare(ware) {
    let ins = new WareSnapshot(ware);
    ins.wareId = ware.id;
    delete ins.id;
    return ins;
  }

  * store() {
    yield Repo.storeSnapshot(this);
  }

}

module.exports = WareSnapshot;